package cn.qihua.dao.impl;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import cn.qihua.dao.ILogOrderDAO;
import cn.qihua.model.BaseOrder;
import cn.qihua.model.LogOrder;
import cn.qihua.model.LogProduct;

/**
 * A data access object (DAO) providing persistence and search support for
 * LogOrder entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.qihua.model.LogOrder
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class LogOrderDAO extends BaseHibernateDAO implements ILogOrderDAO {
	private static final Logger log = LoggerFactory
			.getLogger(LogOrderDAO.class);

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#save(cn.qihua.model.LogOrder)
	 */
	@Override
	public void save(LogOrder transientInstance) {
		log.debug("saving LogOrder instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#delete(cn.qihua.model.LogOrder)
	 */
	@Override
	public void delete(LogOrder persistentInstance) {
		log.debug("deleting LogOrder instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findById(java.lang.Integer)
	 */
	@Override
	public LogOrder findById(java.lang.Integer id) {
		log.debug("getting LogOrder instance with id: " + id);
		try {
			LogOrder instance = (LogOrder) getCurrentSession().get(
					"cn.qihua.model.LogOrder", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findByExample(cn.qihua.model.LogOrder)
	 */
	@Override
	public List findByExample(LogOrder instance) {
		log.debug("finding LogOrder instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("cn.qihua.model.LogOrder")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding LogOrder instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from LogOrder as model where model."
					+ propertyName + "= ? order by inDate desc";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findByNbid(java.lang.Object)
	 */
	@Override
	public List findByNbid(Object nbid) {
		return findByProperty(NBID, nbid);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findByOrderId(java.lang.Object)
	 */
	@Override
	public List findByOrderId(Object orderId) {
		return findByProperty(ORDER_ID, orderId);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findByUserId(java.lang.Object)
	 */
	@Override
	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findByMemo(java.lang.Object)
	 */
	@Override
	public List findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findByIsNew(java.lang.Object)
	 */
	@Override
	public List findByIsNew(Object isNew) {
		return findByProperty(IS_NEW, isNew);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all LogOrder instances");
		try {
			String queryString = "from LogOrder";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#merge(cn.qihua.model.LogOrder)
	 */
	@Override
	public LogOrder merge(LogOrder detachedInstance) {
		log.debug("merging LogOrder instance");
		try {
			LogOrder result = (LogOrder) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#attachDirty(cn.qihua.model.LogOrder)
	 */
	@Override
	public void attachDirty(LogOrder instance) {
		log.debug("attaching dirty LogOrder instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ILogOrderDAO#attachClean(cn.qihua.model.LogOrder)
	 */
	@Override
	public void attachClean(LogOrder instance) {
		log.debug("attaching clean LogOrder instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	@Override
	public Boolean update(LogOrder transientInstance) {
		log.debug("updateing LogOrder instance");
		try {
			getCurrentSession().update(transientInstance);
			log.debug("update successful");
			return true;
		} catch (RuntimeException re) {
			log.error("update failed", re);
			return false;
		} catch (Exception re) {
			log.error("update failed", re);
			return false;
		}
	}
	
	public static ILogOrderDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ILogOrderDAO) ctx.getBean("LogOrderDAO");
	}
}